package com.zk.mockito.cases;

import com.zk.entity.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
//import org.mockito.DoNotMock;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

/**
 * 不需要Mock(模拟)
 */
//@DoNotMock(reason = "Just as an example.")// 这一行使用是错误的，正确的做法在下面
@RunWith(MockitoJUnitRunner.class)
public class Case000_DoNotMocks {
    /**
     * 单车
     */
//    @DoNotMock(reason = "Just as an example.")
    static class Bike {
        private String name = "John";

        public Bike() {

        }

        public Bike(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    @Mock
    private Person person = new Person("Mike");

    /**
     * 当某个类被注解为@DoNotMock时, 如果其自身被Mock会抛出异常如下：
     * org.mockito.exceptions.misusing.DoNotMockException:
     * class com.zk.mockito.cases.Case000_DoNotMocks$Bike
     * is annotated with @org.mockito.DoNotMock and can't be mocked. Just as an example.
     */
    @Mock
    private Bike bike = new Bike();


    /**
     * 不需要模拟测试
     */
    @Test
    public void testDoNotMock() throws ClassNotFoundException {
        Class.forName("com.zk.entity.User", true, ClassLoader.getSystemClassLoader());
        // error: actuall null; 说明Do Not Mock并没有生效
//      Assert.assertEquals("Mike", person.getName());
        Assert.assertNull(person.getName());
        Assert.assertEquals("John", bike.getName());
    }

}
